import React from 'react';
import moment from 'moment';
import { getDisplayText, getDict } from '@/utils/dataDictUtil';
import { MajorInfoCard } from '../components';
import { SEX, LAST_POST_STATUS } from '../dictionary';

export default function TalentMajorInfoCard({ talent = {} }) {
    const [agreeTypeList, setAgreeTypeList] = React.useState([]); // 学历
    // 字典获取
    React.useEffect(() => {
        getDict('agreeType').then(ret => setAgreeTypeList(ret));
    }, []);
    /**
     * 用户信息数据转换，用于MajorInfoCard
     * @param {} data
     */
    const transfer = source => {
        const {
            showReadFlag,
            readFlag,
            talentAvatarUrl,
            talentName,
            talentSex,
            age,
            talentWorkYearNum, // 工作经验年限
            lastPostStatus, // 状态：在职离职
            lastCompany, // 上一家公司
            lastPost, // 工作
            lastCompanyStartTime, // 上一家开始工作时间
            lastCompanyEndTime, // 上一家结束工作时间
            talentGraduateSchool, // 大学
            talentProfession, // 专业
            talentDegree, // 学历
            talentSchoolEntryDate, // 教育开始时间
            talentSchoolGraduateDate, // 教育结束时间
            unMatchRecruitClaimTagNameList = [], // 不匹配标签
            matchRuleTagNameList = [], // 匹配标签
            talentMatchers = [], // 筛选应聘中的岗位列表
            hasBlackList // 是否是黑名单
        } = source;
        const description = [
            getDisplayText(SEX, talentSex),
            age,
            talentWorkYearNum ? `${talentWorkYearNum}年工作经验` : talentWorkYearNum,
            getDisplayText(LAST_POST_STATUS, lastPostStatus)
        ].filter(_ => _);
        // 时间区间格式化
        const formatLastCompanyTime = (from, to, formatStr, split) => {
            if (!from) {
                return '';
            }
            const format = d => moment(d).format(formatStr);
            const f = format(from);
            const t = to ? format(to) : '至今';
            return `${f}${split}${t}`;
        };

        const c = [
            lastCompany,
            lastPost,
            formatLastCompanyTime(lastCompanyStartTime, lastCompanyEndTime, 'YYYY-MM', ' ~ ')
        ].filter(_ => _);
        const education = [
            talentGraduateSchool,
            talentProfession,
            getDisplayText(agreeTypeList, talentDegree),
            formatLastCompanyTime(talentSchoolEntryDate, talentSchoolGraduateDate, 'YYYY-MM', ' - ')
        ].filter(_ => _);
        const applyNum = talentMatchers.filter(item => item.resumeLabel === 1).length;
        return {
            star: showReadFlag && !readFlag,
            avatar: talentAvatarUrl,
            name: talentName,
            description,
            company: c,
            education,
            tags: [
                ...unMatchRecruitClaimTagNameList.map(item => ({ color: 'red', name: item })),
                ...matchRuleTagNameList.map(item => ({ color: 'blue', name: item }))
            ],
            applyNum,
            hasBlackList
        };
    };
    const options = transfer(talent);

    return (
        <div style={{ cursor: 'pointer' }}>
            <MajorInfoCard {...options}></MajorInfoCard>
        </div>
    );
}
